package com.jushu.storbox;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.view.viewImpl.QRImageView;

public class InviteFriendActivity extends BaseTitleActivity {
	private final static String LOG_TAG = "InviteFriendActivity";
	private final static String ANDROID_BARCODE = ClientConfig.SERVICEADD + "/static/clientDownload/androidphone_client.html";
	private final static String IOS_BARCODE = ClientConfig.SERVICEADD + "/clientapp/OneBox_IOS.html";
	private int BARCODE_SIZE;
	private RelativeLayout barcode_android_bt;
	private RelativeLayout barcode_ios_bt;
	private QRImageView barcode_image_iv;
	private boolean isAndroid = true;

	// private RelativeLayout contentLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		LogUtil.i(LOG_TAG, "onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invite_friend);
		switchInvite();
		setCurrentTitle(R.string.invite_friends);
		ActivityTaskManager.getInstance().putActivity(LOG_TAG, InviteFriendActivity.this);
		initView();
		initListener();
		initData();
	}

	@Override
	protected void onLeftClicked(View clickView) {
		InviteFriendActivity.this.finish();
		super.onLeftClicked(clickView);
	}

	private void initData() {
		// TODO Auto-generated method stub
		BARCODE_SIZE = convertDiptoPx(InviteFriendActivity.this, 250);
		barcode_image_iv.updateQr(ANDROID_BARCODE);

	}

	private OnClickListener androidBarcodeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			barcode_image_iv.updateQr(ANDROID_BARCODE);
			if (!isAndroid) {
				barcode_android_bt.setBackgroundResource(R.mipmap.copyright_bt_background_press2);
				barcode_ios_bt.setBackgroundResource(R.mipmap.copyright_bt_background_up2);
				isAndroid = true;
			}

		}
	};

	private OnClickListener iosBarcodeListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			barcode_image_iv.updateQr(IOS_BARCODE);
			if (isAndroid) {
				barcode_android_bt.setBackgroundResource(R.mipmap.copyright_bt_background_up2);
				barcode_ios_bt.setBackgroundResource(R.mipmap.copyright_bt_background_press2);
				isAndroid = false;
			}
		}
	};

	private void initListener() {
		// TODO Auto-generated method stub
		barcode_android_bt.setOnClickListener(androidBarcodeListener);
		barcode_ios_bt.setOnClickListener(iosBarcodeListener);
	}

	private void initView() {
		// TODO Auto-generated method stub
		barcode_image_iv = (QRImageView) findViewById(R.id.barcode_image_iv);
		barcode_android_bt = (RelativeLayout) findViewById(R.id.barcode_android_bt);
		barcode_ios_bt = (RelativeLayout) findViewById(R.id.barcode_ios_bt);
		// contentLayout=(RelativeLayout)
		// findViewById(R.id.invite_friend_content);
		// RelativeLayout.LayoutParams
		// params=(android.widget.RelativeLayout.LayoutParams)
		// barcode_logo_iv.getLayoutParams();
		// params.setMargins(0,
		// convertDiptoPx(this,50)+BARCODE_SIZE/2-convertDiptoPx(this,50)/2, 0,
		// 0);
	}

	// Px to Dip
	public static int convertPxToDip(Context context, int px) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1));
	}

	// Dip to Px
	public static int convertDiptoPx(Context context, int dip) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		LogUtil.i(LOG_TAG, "onDestroy()");
		super.onDestroy();
		ActivityTaskManager.getInstance().removeActivity(LOG_TAG);
	}
}
